﻿/* 
 * DiagnosticsControl.cs
 * 
 * Author - Ankit Gupta and Gaurav Aggarwal
 * 
 * This file acts as a controller to Diagnostics tab.
 * 
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Security;
using System.Diagnostics;

partial class DiagnosticsControl : UserControl
{
    // Apply the changes
    private void applyButton_Click(object sender, EventArgs e)
    {
        try
        {
            // Validate whether entered data is in proper format or not
            if (validatedata() == false)
                return;

            // Enable Logging
            if (enableTracingCheckBox.Checked)
            {
                serviceParameters.SetValue("TraceOption", (int)serviceParameters.GetValue("TraceOption", 0) | 8, RegistryValueKind.DWord);
            }
            else
            {
                serviceParameters.SetValue("TraceOption", (int)serviceParameters.GetValue("TraceOption", 0) & 7, RegistryValueKind.DWord);
            }

            // Send trace as debug output
            if (traceAsDebugOutputCheckBox.Checked)
            {
                serviceParameters.SetValue("TraceOption", (int)serviceParameters.GetValue("TraceOption", 0) | 4, RegistryValueKind.DWord);
            }
            else
            {
                serviceParameters.SetValue("TraceOption", (int)serviceParameters.GetValue("TraceOption", 0) & 11, RegistryValueKind.DWord);
            }

            // Num entries to keep in log
            if ("5000".Equals(logEntriesTextBox.Text))
            {
                serviceParameters.DeleteValue("TraceBufferSize", false);
            }
            else
            {
                serviceParameters.SetValue("TraceBufferSize", Int32.Parse(logEntriesTextBox.Text), RegistryValueKind.DWord);
            }

            // Max size of log file
            if ("100".Equals(logSizeTextBox.Text))
            {
                serviceParameters.DeleteValue("MaxLogSize", false);
            }
            else
            {
                serviceParameters.SetValue("MaxLogSize", Int32.Parse(logSizeTextBox.Text));
            }

            // Report SMB session startups
            if (reportStartupCheckBox.Checked)
            {
                serviceParameters.SetValue("ReportSessionStartups", 1, RegistryValueKind.DWord);
            }
            else
            {
                serviceParameters.DeleteValue("ReportSessionStartups", false);
            }

            // Issue a debug breakpoint
            if (breakpointOnPanicCheckBox.Checked)
            {
                serviceParameters.SetValue("TrapOnPanic", 1, RegistryValueKind.DWord);
            }
            else
            {
                serviceParameters.DeleteValue("TrapOnPanic", false);
            }

            // Verify Signatures
            if (signatureVerificationCheckBox.Checked)
            {
                clientParameters.DeleteValue("VerifyServiceSignature", false);
            }
            else
            {
                clientParameters.SetValue("VerifyServiceSignature", 0, RegistryValueKind.DWord);
            }

            // Debug AFS IOCTL Calls
            if (debugCallsCheckBox.Checked)
            {
                clientParameters.SetValue("IoctlDebug", 1, RegistryValueKind.DWord);
            }
            else
            {
                clientParameters.DeleteValue("IoctlDebug", false);
            }
        }
        catch (SecurityException)
        {
            MessageBox.Show("You do not have access to apply these settings.", "Access Denied", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    // Cancel the operations
    private void cancelButton_Click(object sender, EventArgs e)
    {
        Process[] p = Process.GetProcessesByName("mmc");
        p[0].Kill();
    }

    // Error checking
    private bool validatedata()
    {
        if (!checkint(logEntriesTextBox.Text))
        {
            String error = "Number of Log Entries should be an integer";
            MessageBox.Show(error);
            return false;
        }
        if (!checkint(logSizeTextBox.Text))
        {
            String error = "Maximum Size of Log File should be an integer";
            MessageBox.Show(error);
            return false;
        }
        return true;
    }

    private bool checkint(string a)
    {
        bool flag = true;

        if (a.Equals("")) flag = false;

        for (int i = 0; i < a.Length; i++)
        {
            if (a[i] < 48 || a[i] > 57) flag = false;
        }
        return flag;
    }
}